<?php

namespace app\supplier\model\product;

use think\facade\Cache;
use app\common\model\product\Category as CategoryModel;
use app\common\model\supplier\CategoryStatus as CategoryStatusModel;

/**
 * 商品分类模型
 */
class Category extends CategoryModel
{
	
	/**
	 * @Exp    商户管理-获取分类列表
	 * @Author Mr.Z
	 * @param  [type]     $where            [description]
	 * @param  integer    $shop_supplier_id [description]
	 * @return [type]                       [description]
	 */
	public function getCategorytree($where,$shop_supplier_id=0){
		return $this->getLists($where,$shop_supplier_id)['tree'];
	}

	/**
	 * @Exp    联合表查询
	 * @Author Mr.Z
	 * @param  [type]     $where            [description]
	 * @param  integer    $shop_supplier_id [description]
	 * @return [type]                       [description]
	 */
	public function getLists($where,$shop_supplier_id=0){
		$base = ['audit_status' => 1];
		if ($where) {
			$base = array_merge($base, $where);
		}
		$model = $this->where($base);
//		$Cmodel = (new CategoryStatusModel())->alias('s')->where('s.shop_supplier_id','=',$shop_supplier_id);
		//UNION 合并表
		$bqsql = (new CategoryStatusModel())->alias('s')->where('s.shop_supplier_id','=',$shop_supplier_id)->join('category c','c.category_id=s.category_id and c.parent_id>0')->field('s.category_id as category_id,c.name as name,s.show_status as is_show,s.sort as sort,c.parent_id as parent_id,c.type as type')->buildSql();
        //UNION 合并表2
        $cqsql = (new CategoryStatusModel())->alias('s')->where('s.shop_supplier_id','=',$shop_supplier_id)->join('category c','c.category_id=s.category_id and c.parent_id=0')->field('s.category_id as category_id,c.name as name,s.show_status as is_show,c.sort as sort,c.parent_id as parent_id,c.type as type')->buildSql();
		//排除表
		$cs_ids = (new CategoryStatusModel())->alias('s')->where('s.shop_supplier_id','=',$shop_supplier_id)->column('s.category_id as category_id');

		$data = $model->with(['images'])->alias('a')
			->where('category_id','not in',$cs_ids)
			->where(['a.is_show'=>1])
			->order(['type'=>'desc','sort' => 'asc', 'category_id' => 'asc'])
			->union($bqsql)
            ->union($cqsql)
			->field('category_id,name,0 as is_show,sort,parent_id,type')
			->select();
		
		$all = !empty($data) ? $data->toArray() : [];

		$tree = [];
		foreach ($all as $first) {
			if ($first['parent_id'] != 0) continue;
			$twoTree = [];
			foreach ($all as $two) {
				if ($two['parent_id'] != $first['category_id']) continue;
				$threeTree = [];
				foreach ($all as $three)
					$three['parent_id'] == $two['category_id']
					&& $threeTree[$three['category_id']] = $three;
				!empty($threeTree) && $two['child'] = $threeTree;
				array_push($twoTree, $two);
			}
			if (!empty($twoTree)) {
				$temp_two_tree = array_column($twoTree, 'sort');
				array_multisort($temp_two_tree, SORT_ASC, $twoTree);
				$first['child'] = $twoTree;
			}
			array_push($tree, $first);
		}

		return compact('all', 'tree');
	}

	/**
	 * @Exp    编辑状态
	 * @Author Mr.Z
	 * @Date   2024-05-11
	 * @param  [type]     $postData         [description]
	 * @param  integer    $shop_supplier_id [description]
	 */
	public function setStatus($postData,$shop_supplier_id=0)
	{
		$formlist = $postData['form'];
		$oknum = 0;
		if($formlist){
			foreach ($formlist as $key => $value) {
				$category_id = $value['category_id'];
				//保存1开始
				$value['sort'] = $key+1;
				$model = CategoryStatusModel::findCategory($category_id,$shop_supplier_id);
				if($model){
					if($model['show_status']==$value['is_show']&&$model['sort']==$value['sort']){
						continue;
					}else{
						if($model->save($this->setData($value))){
							$oknum++;
						}
					}
				}else{
					if((new CategoryStatusModel())->save($this->setData($value,$shop_supplier_id,1))){
						$oknum++;
					}
				}
			}
		}
		if ($oknum>0) {
			return true;
		}
		return false;
	}

	/**
	 * @Exp    重组数据
	 * @Author Mr.Z
	 * @Date   2024-05-11
	 * @param  [type]     $data             [description]
	 * @param  integer    $shop_supplier_id [description]
	 * @param  integer    $type             [description]
	 */
	public function setData($data,$shop_supplier_id=0,$type=0){
		$savedata = [];
		$savedata['show_status'] = $data['is_show'];
		$savedata['sort'] = $data['sort'];
		$savedata['update_time'] = time();
		if($type==1){
			$savedata['category_id'] = $data['category_id'];
			$savedata['shop_supplier_id'] = $shop_supplier_id;
			$savedata['create_time'] = time();
			$savedata['app_id'] = self::$app_id;
		}
		return $savedata;
	}
}
